﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;

namespace WinFromBook
{
    public partial class FrmBorrowhistory : CCSkinMain
    {
        public FrmBorrowhistory()
        {
            InitializeComponent();
        }
        #region 封装方法，执行操作
        //返回数据
        public DataTable LoadAns(string ReadName, string BookName)
        {
            //加载所有借阅记录
            DataTable dt = new BLL.borrowerIfnoBLL().GetBorrwerBookReader(ReadName, BookName);

            return dt;
        }
        #region 判断返回数据是否为null
        public void CheckInfo(string name, string Bkname)
        {
            if (LoadAns(name, Bkname).Rows.Count == 0)
            {
                MessageBox.Show("查询不到记录");
                //加载全部借阅记录
                this.dataGridViewBorrower.DataSource = LoadAns(null, null);
            }
        }
        #endregion
        //根据读者姓名或图书名称查询
        public void NameOrBook()
        {
            if (radioReader.Checked)
            {
                //重新加载DataGridView
                this.dataGridViewBorrower.DataSource = LoadAns(String.IsNullOrEmpty(textReader.Text.Trim()) ? null : textReader.Text.Trim(), null);
                //调用判断
                CheckInfo(String.IsNullOrEmpty(textReader.Text.Trim()) ? null : textReader.Text.Trim(), null);
            }
            else if (radioBookName.Checked)
            {
                this.dataGridViewBorrower.DataSource = LoadAns(null, String.IsNullOrEmpty(textBookName.Text.Trim()) ? null : textBookName.Text.Trim());
                CheckInfo(null, String.IsNullOrEmpty(textBookName.Text.Trim()) ? null : textBookName.Text.Trim());
            }
            //清空文本
            this.textBookName.Text = "";
            this.textReader.Text = "";
        }
        //按时间查询
        public void GetAnsDate()
        {
            string Date = this.dateTimePicker1.Value.Year + "/" + this.dateTimePicker1.Value.Month + "/" + this.dateTimePicker1.Value.Day;
            string date = this.dateTimePicker2.Value.Year + "/" + this.dateTimePicker2.Value.Month + "/" + this.dateTimePicker2.Value.Day;
            //传递日期查询数据
            DataTable dt = new BLL.borrowerIfnoBLL().GetBorrwerData(Date, date);
            //重新加载DataGridView
            this.dataGridViewBorrower.DataSource = dt;
        }
        //还书方法
        public void ReturnBook()
        {
            if (this.dataGridViewBorrower.SelectedRows[0].Cells[4].Value.ToString() != "")
            {
                MessageBox.Show("该图书已归还", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            FrmBorrwer fb = new FrmBorrwer();
            fb.BorrowerID = this.dataGridViewBorrower.SelectedRows[0].Cells[0].Value.ToString();
            fb.Text = "还书登记";
            fb.FB = this;
            fb.Show();
        }
        public void GetInfo()
        {
            this.dataGridViewBorrower.DataSource = LoadAns(null, null);
        }
        #endregion
        public  void FrmBorrowhistory_Load(object sender, EventArgs e)
        {
            GetInfo();
        }

        private void buttonAns_Click(object sender, EventArgs e)
        {
            NameOrBook();
        }

        private void buttoDate_Click(object sender, EventArgs e)
        {
            GetAnsDate();
        }

        private void 还书登记ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReturnBook();
        }
    }
}
